package com.gali.juc;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Auther: gali
 * @Date: 2022-08-23 15:56
 * @Description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
class Book{
    private int id;
    private String bookName;
}
public class AtomicStampedDemo {
    public static void main(String[] args) {
        Book java = new Book(1, "Java");
        //版本号
        AtomicStampedReference<Book> stampedReference= new AtomicStampedReference<>(java,1);
        System.out.println(stampedReference.getReference()+"\t"+stampedReference.getStamp());
        Book mysql = new Book(2, "mysql");
        boolean b = stampedReference.compareAndSet(java, mysql, stampedReference.getStamp(), stampedReference.getStamp() + 1);
        System.out.println(b+"\t"+stampedReference.getReference()+"\t"+stampedReference.getStamp());
        boolean b1 = stampedReference.compareAndSet(mysql, java, stampedReference.getStamp(), stampedReference.getStamp() + 1);
        System.out.println(b1+"\t"+stampedReference.getReference()+"\t"+stampedReference.getStamp());
    }
}
